package com.ihrm.system.controller;

import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.PageResult;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.domain.system.Role;
import com.ihrm.domain.system.response.RoleResult;
import com.ihrm.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 13932105637
 * @version l.0
 * @date 2021/7/14 23:14
 *
 * 1.解决跨域  CrossOrigin
 * 2.声明restContoller RestController
 * 3.设置父路径 RequestMapping
 **/
@CrossOrigin
@RestController
@RequestMapping(value="/sys")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService iRoleService;

    /**
     * 添加角色
     **/
    @RequestMapping(value = "/role", method = RequestMethod.POST)
    public Result add(@RequestBody Role role)throws Exception{
        role.setCompanyId(companyId);
        iRoleService.save(role);
        return Result.SUCCESS();
    }

    /**
     * 更新角色
     **/
    @RequestMapping(value = "/role/{id}",method = RequestMethod.PUT)
    public Result update(@PathVariable(name = "id")String id,@RequestBody Role role) throws Exception{
        iRoleService.update(role);
        return Result.SUCCESS();
    }

    /**
     * 删除角色
     **/
    @RequestMapping(value = "/role/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        iRoleService.delete(id);
        return Result.SUCCESS();
    }

    /**
     * 根据ID获取角色信息
     **/
    @RequestMapping(value = "/role/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) throws Exception{
        Role role = iRoleService.findById(id);
        RoleResult roleResult = new RoleResult(role);
        return new Result(ResultCode.SUCCESS,roleResult);
    }

    /**
     * 分页查询角色
     **/
    @RequestMapping(value = "/role", method = RequestMethod.GET)
    public Result findByPage(int page, int pagesize, Role role) {
        Page<Role> searchPage = iRoleService.findByPage(companyId,page, pagesize);
        PageResult pageResult = new PageResult(searchPage.getTotalElements(), searchPage.getContent());
        return new Result(ResultCode.SUCCESS, pageResult);
    }

    @RequestMapping(value = "/role/list", method = RequestMethod.GET)
    public Result findAll() throws Exception{
        List<Role> roleList = iRoleService.findAll(companyId);
        return new Result(ResultCode.SUCCESS, roleList);
    }

}
